﻿using System.Linq;
using System.Xml;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Cliente
{
    public class DatosCliente
    {
        public int PuertoUDP { get; set; }
        public string FileRepository { get; set; }
        public string IPCliente { get; set; }
        public int TimeoutDescubrimiento { get; set; }

        private static DatosCliente _instancia = null;
        
        public static DatosCliente Instancia 
        { 
            get
            {
                if (_instancia == null)
                {
                    _instancia = new DatosCliente();
                }
                return _instancia;
            }
        }
        
        
        protected DatosCliente()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(string.Format("{0}\\{1}", Directory.GetCurrentDirectory(), "clientConf.xml"));
            doc.PreserveWhitespace = false;

            this.PuertoUDP = int.Parse(doc.SelectSingleNode("conf/udp-port/text()").Value);
            this.FileRepository = doc.SelectSingleNode("conf/file-repository/text()").Value;
            this.TimeoutDescubrimiento = int.Parse(doc.SelectSingleNode("conf/timeout-descubrimiento/text()").Value) * 1000;

            this.IPCliente = (from addr in Dns.GetHostEntry(Dns.GetHostName()).AddressList
                               where addr.AddressFamily == AddressFamily.InterNetwork
                               select addr.ToString()).FirstOrDefault();
        }
    }
}
